home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 114 / PC Guia 114.iso / Software / Utils / The Gimp 2.2.1 / gimp-help-2-0.6-setup.exe / {app} / share / gimp / 2.0 / help / en / ch02s10s05.html < prev    next >
Encoding:
Extensible Markup Language  |  2004-12-19  |  12.3 KB  |  317 lines

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4.   <head>
  5.     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  6.     <title>10.5.┬áGiving Our Script Some Guts</title>
  7.     <link rel="stylesheet" href="gimp-help-plain.css" type="text/css" />
  8.     <link rel="stylesheet" href="gimp-help-screen.css" type="text/css" />
  9.     <meta name="generator" content="DocBook XSL Stylesheets V1.66.1" />
  10.     <link rel="start" href="index.html" title="GIMP User Manual" />
  11.     <link rel="up" href="ch02s10.html" title="10.┬áA Script-Fu Tutorial" />
  12.     <link rel="prev" href="ch02s10s04.html" title="10.4.┬áYour First Script-Fu Script" />
  13.     <link rel="next" href="ch02s10s06.html" title="10.6.┬áExtending The Text Box Script" />
  14.   </head>
  15.   <body>
  16.     <div xmlns="" class="navheader">
  17.       <table width="100%" summary="Navigation header">
  18.         <tr>
  19.           <th colspan="3" align="center" id="chaptername">10.┬áA Script-Fu Tutorial</th>
  20.         </tr>
  21.         <tr>
  22.           <td width="20%" align="left"><a accesskey="p" href="ch02s10s04.html">Prev</a>┬á</td>
  23.           <th width="60%" align="center" id="sectionname">10.5.┬áGiving Our Script Some Guts</th>
  24.           <td width="20%" align="right">┬á<a accesskey="n" href="ch02s10s06.html">Next</a></td>
  25.         </tr>
  26.       </table>
  27.       <hr />
  28.     </div>
  29.     <div class="sect2" lang="en" xml:lang="en">
  30.       <div class="titlepage">
  31.         <div>
  32.           <div>
  33.             <h3 class="title"><a id="id3430769"></a>10.5.┬áGiving Our Script Some Guts</h3>
  34.           </div>
  35.         </div>
  36.       </div>
  37.       <p>
  38.       Let us continue with our training and add some functionality to
  39.       our script. 
  40.     </p>
  41.       <div class="simplesect" lang="en" xml:lang="en">
  42.         <div class="titlepage">
  43.           <div>
  44.             <div>
  45.               <h4 class="title"><a id="id3430782"></a>Creating A New Image</h4>
  46.             </div>
  47.           </div>
  48.         </div>
  49.         <p>
  50.         In the previous lesson, we created an empty function and
  51.         registered it with Gimp. In this lesson, we want to provide
  52.         functionality to our script -- we want to create a new image,
  53.         add the user's text to it and resize the image to fit the text
  54.         exactly. 
  55.       </p>
  56.         <p>
  57.         Once you know how to set variables, define functions and
  58.         access list members, the rest is all downhill -- all you need
  59.         to do is familiarize yourself with the functions available in
  60.         Gimp's procedural database and call those functions
  61.         directly. So fire up the DB Browser and let's get cookin'! 
  62.       </p>
  63.         <p>
  64.         Let's begin by making a new image. We'll create a new
  65.         variable, <tt class="varname">theImage</tt>, set to the result of calling Gimp's
  66.         built-in function <tt class="code">gimp-image-new</tt>. 
  67.       </p>
  68.         <p>
  69.       As you can see from the DB Browser, the function
  70.       <tt class="code">gimp-image-new</tt> takes three parameters -- the
  71.       image's width, height and the type of image. Because we'll
  72.       later resize the image to fit the text, we'll make a 10x10 RGB
  73.       image. We'll store the image's width and sizes in some
  74.       variables, too, as we'll refer to and manipulate them later in
  75.       the script. 
  76.       </p>
  77.         <pre class="programlisting">
  78.         (define (script-fu-text-box inTest inFont inFontSize inTextColor)
  79.         (let*
  80.               (
  81.                  ; define our local variables
  82.                  ; create a new image:
  83.                  (theImageWidth  10)
  84.                  (theImageHeight 10)
  85.                  (theImage (car 
  86.                                 (gimp-image-new
  87.                                  theImageWidth
  88.                                  theImageHeight
  89.                                  RGB
  90.                                 )
  91.                            )
  92.                  )
  93.                  (theText)     ;a declaration for the text
  94.                                ;we create later
  95.       </pre>
  96.         <p>
  97.         Note: We used the value RGB to specify that the image is an
  98.         RGB image. We could have also used 0, but RGB is more
  99.         descriptive when we glance at the code. 
  100.       </p>
  101.         <p>
  102.         You should also notice that we took the head of the result of
  103.         the function call. This may seem strange, because the database
  104.         explicitly tells us that it returns only one value -- the ID
  105.         of the newly created image. However, all Gimp functions return
  106.         a list, even if there is only one element in the list, so we
  107.         need to get the head of the list. 
  108.       </p>
  109.       </div>
  110.       <div class="simplesect" lang="en" xml:lang="en">
  111.         <div class="titlepage">
  112.           <div>
  113.             <div>
  114.               <h4 class="title"><a id="id3430861"></a>Adding A New Layer To The Image</h4>
  115.             </div>
  116.           </div>
  117.         </div>
  118.         <p>
  119.         Now that we have an image, we need to add a layer to it. We'll
  120.         call the <tt class="code">gimp-layer-new</tt> function to create the
  121.         layer, passing 
  122.         in the ID of the image we just created. (From now on, instead
  123.         of listing the complete function, we'll only list the lines
  124.         we're adding to it. You can see the complete script here.)
  125.         Because we've declared all of the local variables we'll use,
  126.         we'll also close the parentheses marking the end of our
  127.         variable declarations: 
  128.       </p>
  129.         <pre class="programlisting">
  130.         ;create a new layer for the image:
  131.            (theLayer 
  132.                      (car
  133.                           (gimp-layer-new
  134.                            theImage
  135.                            theImageWidth
  136.                            theImageHeight
  137.                            RGB_IMAGE
  138.                            "layer 1"
  139.                            100
  140.                            NORMAL
  141.                           )
  142.                       )
  143.             )
  144.          ) ;end of our local variables
  145.       </pre>
  146.         <p>
  147.         Once we have the new layer, we need to add it to the image:
  148.       </p>
  149.         <pre class="programlisting">
  150.        (gimp-image-add-layer theImage theLayer 0)
  151.       </pre>
  152.         <p>
  153.         Now, just for fun, let's see the fruits of our labors up until
  154.         this point, and add this line to show the new, empty image: 
  155.       </p>
  156.         <pre class="programlisting">
  157.         (gimp-display-new theImage)
  158.       </pre>
  159.         <p>
  160.        Save your work, select 
  161.        <span class="guimenu">Xtns</span>-><span class="guisubmenu">Script-Fu</span>-><span class="guimenuitem">Refresh</span>, 
  162.        run the script and a new image should pop up. It will probably
  163.        contain garbage (random colors), because we haven't erased
  164.        it. We'll get to that in a second. 
  165.       </p>
  166.       </div>
  167.       <div class="simplesect" lang="en" xml:lang="en">
  168.         <div class="titlepage">
  169.           <div>
  170.             <div>
  171.               <h4 class="title"><a id="id3430948"></a>Adding The Text</h4>
  172.             </div>
  173.           </div>
  174.         </div>
  175.         <p>
  176.         Go ahead and remove the line to display the image (or comment
  177.         it out with a ; as the first character of the line). 
  178.       </p>
  179.         <p>
  180.         Before we add text to the image, we need to set the background
  181.         and foreground colors so that the text appears in the color
  182.         the user specified. We'll use the
  183.         gimp-palette-set-back/foreground functions: 
  184.       </p>
  185.         <pre class="programlisting">
  186.         (gimp-palette-set-background '(255 255 255) )
  187.         (gimp-palette-set-foreground inTextColor)
  188.       </pre>
  189.         <p>
  190.         With the colors properly set, let's now clean out the garbage
  191.         currently in the image. We'll select everything in the image,
  192.         and call clear: 
  193.       </p>
  194.         <pre class="programlisting">
  195.         (gimp-selection-all theImage)
  196.         (gimp-edit-clear theImage theLayer)
  197.         (gimp-selection-none theImage)
  198.       </pre>
  199.         <p>
  200.         With the image cleared, we're ready to add some text:
  201.       </p>
  202.         <pre class="programlisting">
  203.         (set! theText
  204.                       (car
  205.                            (gimp-text
  206.                             theImage
  207.                             theLayer
  208.                             0 0
  209.                             inText
  210.                             0
  211.                             TRUE
  212.                             inFontSize
  213.                             PIXELS
  214.                             "*" "*" "*"
  215.                            )
  216.                        )
  217.         )  
  218.       </pre>
  219.         <p>
  220.         Although a long function call, it's fairly straightforward if
  221.         you go over the parameters while looking at the function's
  222.         entry in the DB Browser. Basically, we're creating a new text
  223.         layer and assigning it to the variable
  224.         <tt class="varname">theText</tt>.  
  225.       </p>
  226.         <p>
  227.         Now that we have the text, we can grab its width and height
  228.         and resize the image and the image's layer to the text's size: 
  229.       </p>
  230.         <pre class="programlisting">
  231.         (set! theImageWidth   (car (gimp-drawable-width  theText) ) )      
  232.         (set! theImageHeight  (car (gimp-drawable-height theText) ) )
  233.  
  234.         (gimp-image-resize theImage theImageWidth theImageHeight 0 0)    
  235.       
  236.         (gimp-layer-resize theLayer theImageWidth theImageHeight 0 0)
  237.       </pre>
  238.         <p>
  239.         If you're like me, you're probably wondering what a drawable
  240.         is when compared to a layer. The difference between the two is
  241.         that a drawable is anything that can be drawn into, including
  242.         layers but also channels, layer masks, the selection, etc; a
  243.         layer is 
  244.         a more specific version of a drawable. In most cases, the
  245.         distinction is not important. 
  246.       </p>
  247.         <p>
  248.        With the image ready to go, we can now re-add our display line:
  249.       </p>
  250.         <pre class="programlisting">
  251.         (gimp-display-new theImage)
  252.       </pre>
  253.         <p>
  254.         Save your work, refresh the database and give your first
  255.         script a run!
  256.       </p>
  257.       </div>
  258.       <div class="simplesect" lang="en" xml:lang="en">
  259.         <div class="titlepage">
  260.           <div>
  261.             <div>
  262.               <h4 class="title"><a id="id3431055"></a>Clearing The Dirty Flag</h4>
  263.             </div>
  264.           </div>
  265.         </div>
  266.         <p>
  267.         If you try to close the image created without first saving the
  268.         file, Gimp will ask you if you want to save your work before
  269.         you close the image. It asks this because the image is marked
  270.         as dirty, or unsaved. In the case of our script, this is a
  271.         nuisance for the times when we simply give it a test run and
  272.         don't add or change anything in the resulting image -- that
  273.         is, our work is easily reproducible in such a simple script,
  274.         so it makes sense to get rid of this dirty flag. 
  275.       </p>
  276.         <p>
  277.         To do this, we can clear the dirty flag after displaying the
  278.         image: 
  279.       </p>
  280.         <pre class="programlisting">
  281.         (gimp-image-clean-all theImage)
  282.       </pre>
  283.         <p>
  284.         This will set dirty count to 0, making it appear to be a
  285.         "clean" image. 
  286.       </p>
  287.         <p>
  288.         Whether to add this line or not is a matter of personal
  289.         taste. I use it in scripts that produce new images, where the
  290.         results are trivial, as in this case. If your script is very
  291.         complicated, or if it works on an existing image, you will
  292.         probably not want to use this function. 
  293.       </p>
  294.       </div>
  295.     </div>
  296.     <div class="navfooter">
  297.       <hr />
  298.       <table width="100%" summary="Navigation footer">
  299.         <tr>
  300.           <td width="40%" align="left"><a accesskey="p" href="ch02s10s04.html">Prev</a>┬á</td>
  301.           <td width="20%" align="center">
  302.             <a accesskey="u" href="ch02s10.html">Up</a>
  303.           </td>
  304.           <td width="40%" align="right">┬á<a accesskey="n" href="ch02s10s06.html">Next</a></td>
  305.         </tr>
  306.         <tr>
  307.           <td width="40%" align="left" valign="top">10.4.┬áYour First Script-Fu Script┬á</td>
  308.           <td width="20%" align="center">
  309.             <a accesskey="h" href="index.html">Home</a>
  310.           </td>
  311.           <td width="40%" align="right" valign="top">┬á10.6.┬áExtending The Text Box Script</td>
  312.         </tr>
  313.       </table>
  314.     </div>
  315.   </body>
  316. </html>
  317.